// 导入静态路由表和全部的动态路由数组
import { constantRoutes, asyncRoutes } from '@/router'
const getDefaultState = () => {
    return {
        // 用来保存最终的路由表
        // 包含静态 + 静态，但是默认只有静态
        routes: [...constantRoutes]
    }
}

const state = getDefaultState()


// mutations
const mutations = {

    // 第二个参数就是传递过来的筛选后的动态路由
    setRoutes(state, routes) {
        // 最终路由有 静态 + 筛选后的动态
        state.routes = [...constantRoutes, ...routes]
    }
}

// actions
const actions = {

    getRoutes(ctx, menus) {

        // 根据权限名过滤出动态数组里的路由
        const routes = asyncRoutes.filter(v => menus.includes(v.name))
         // 404 page must be placed at the end !!!
        routes.push( { path: '*', redirect: '/404', hidden: true } )
        ctx.commit('setRoutes', routes)
        return routes
    }
}

export default {

    namespaced: true,

    state,
    mutations,
    actions
}